module.exports = function (args) {
  args = args || {};
  this.idKey = args.idKey || 'id';
  this.pageSize = args.pageSize || 10;
  this.pageNum = args.pageNum || 1;
  this.total = args.total || 0;
  this.rows = args.rows || [];
  this.show = typeof args.showFn == 'function' ? args.showFn : function () {}
  this.disClicks = args.disClicks || '';
  this.SQLite = args.data ? new a.SQLite(args.data) : '';
  this.get = args.get || '';
  this.loadFn = '';
  this.res = {
    pageSize: 10,
    pageNum: 0,
    rows: [],
    total: 0
  };
  this.beforeLoadFn = args.beforeLoadFn || '';
  this.status = args.status || {
    allLoaded: false,
    loading: false,
    refreshing: false,
    noData: false,
    beforeLoad: true,
    loadErr: false
  }
  this.allChecked = args.allChecked || false;
  this.checkNum = args.checkNum || 0;
  this.init = function (args) {
    args = args || {};
    a.merge(this.status, {
      allLoaded: false,
      loading: false,
      refreshing: false,
      noData: false,
      beforeLoad: true,
      loadErr: false
    })
    this.allChecked = false;
    this.checkNum = 0;
    this.res = {};
    this.rows.splice(0, this.rows.length);
    if (args.rows) a.merge(this.rows, args.rows);
    this.pageSize = args.pageSize || 10;
    this.pageNum = args.pageNum || 1;
    this.total = args.total || 0;
  }
  this.refresh = function (fn, n) {
    if (this.status.loading) return false;
    if (this.status.refreshing) return false;
    this.pageNum = n || 1;
    this.status.refreshing = true;
    this.status.allLoaded = false;
    this.allChecked = false;
    this.checkNum = 0;
    this.load(fn);
  }
  this.load = function (fn) {
    var self = this;
    if (this.status.loading) return false;
    if (this.status.allLoaded) return false;
    if (!this.status.refreshing) this.status.loading = true;
    if (this.status.beforeLoad) this.status.beforeLoad = false;
    if (this.status.noData) this.status.noData = false;
    if (typeof this.beforeLoadFn == 'function') this.beforeLoadFn.call(this);
    if (this.SQLite) {
      this.SQLite.find(this.get.data, function (res) {
        setTimeout(function () {
          self.add(res, fn);
        }, 100)
      }, {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      });
    } else if (this.get) {
      this.get.data.pageNum = this.pageNum;
      this.get.data.pageSize = this.pageSize;
      this.get.action('', function (res) {
        self.add(res, fn);
      })
    }
  }
  this.add = function (res, fn) {
    this.res = res;
    if (this.status.refreshing) this.rows.splice(0, this.rows.length);
    if (res && res.rows instanceof Array) {
      this.status.loadErr = false;
      this.total = res.total;
      if (res.rows.length > 0) {
        for (var i = 0; i < res.rows.length; i++) {
          if (typeof args.addDataChange == 'function') args.addDataChange.call(res.rows[i]);
          if (typeof args.dataFormat == 'function') args.dataFormat(res.rows[i]);
          if (this.disClicks && this.idKey && this.disClicks.indexOf('|' + res.rows[i][this.idKey] + '|') > -1) res.rows[i].disClick = true;
          res.rows[i].checked = res.rows[i].checked || false;
          this.rows.push(res.rows[i]);
        }
      }
      if (this.total == 0) this.status.noData = true;
      if (this.total == this.rows.length) this.status.allLoaded = true;
      this.pageNum++;
    } else {
      this.status.loadErr = true;
    }
    this.status.loading = false;
    this.status.refreshing = false;
    if (typeof this.show == 'function') this.show.call(this, this.getData(), 'add');
    if (typeof fn == 'function') fn.call(this);
  }
  this.checkn = function (n, fn) {
    if (this.status.loading || this.status.refreshing) return false;
    if (this.disClicks && this.disClicks.indexOf('|' + this.rows[n][this.idKey] + '|') > -1) return false;
    if (typeof fn == 'function') {
      if (fn.call(this, this.rows[n])) {
        this.rows[n].checked = !this.rows[n].checked;
        this.checkNum += this.rows[n].checked ? 1 : -1;
        this.allChecked = this.checkNum == this.rows.length;
        this.show.call(this, this.getData(), 'checkn');
      }
    } else {
      this.rows[n].checked = !this.rows[n].checked;
      this.checkNum += this.rows[n].checked ? 1 : -1;
      this.allChecked = this.checkNum == this.rows.length;
      this.show.call(this, this.getData(), 'checkn');
    }
  }
  this.checkAll = function (bool, fn) {
    if (this.status.loading || this.status.refreshing) return false;
    for (var i = 0; i < this.rows.length; i++) {
      if(typeof fn == 'function'){
        if(fn.call(this,this.rows[i])){
          if (this.disClicks && this.disClicks.indexOf('|' + this.rows[i][this.idKey] + '|') > -1) continue;
          this.rows[i].checked = !!bool;
        }
      }else{
        if (this.disClicks && this.disClicks.indexOf('|' + this.rows[i][this.idKey] + '|') > -1) continue;
        this.rows[i].checked = !!bool;
      }
    }
    this.allChecked = !!bool;
    this.checkNum = this.allChecked ? this.rows.length : 0;
    this.show.call(this, this.getData(), 'checkAll');
  }
  this.getData = function () {
    return {
      status: this.status,
      allChecked: this.allChecked,
      rows: this.rows,
      pageNum: this.pageNum,
      pageSize: this.pageSize,
      total: this.total,
      checkNum: this.checkNum
    }
  }
  this.val = function (s) {
    if (s) {
      for (var i = 0; i < this.rows.length; i++) {
        if (s[this.rows[i][this.idKey]]) {
          this.rows[i].checked = true;
          this.checkNum++;
        }
      }
      if (this.checkNum == this.rows.length) this.allChecked = true;
      this.show.call(this, this.getData(), 'val');
    } else {
      var rc = [];
      for (var i = 0; i < this.rows.length; i++)
        if (this.rows[i].checked) rc.push(this.rows[i]);
      return rc;
    }
  }
  //this.init(args);
}